package com.yznote.cloudesk.logs.controller;

import com.yznote.cloudesk.common.pojo.Result;
import com.yznote.cloudesk.common.pojo.YLogs;
import com.yznote.cloudesk.logs.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;
import java.util.List;

@RestController
@RequestMapping("/log")
@Api("日志系统")
public class LogController {
    @Autowired
    LogService logService;

    @ApiOperation("分页查询日志")
    @GetMapping("/queryLog")
    public Result queryLog(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<YLogs> yLogsList = logService.queryLogs(pageSize, pageNum);
        return Result.success(yLogsList);
    }

    @ApiOperation("/查询某秒内的总的接口访问数量")
    @GetMapping("/getNumInSec")
    public Result getNumInSec(@RequestParam("start") Long times) {
        // 传入起始的时间戳
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(times), ZoneOffset.of("+8"));
        Integer num = logService.getNumInSec(localDateTime);
        return Result.success(num);
    }

    @ApiOperation("/查询一天的网站访问趋势")
    @GetMapping("/getNumByDay")
    public Result getNumByDay() {
        // 从今天0小时开始
        List<Integer> nums = logService.getNumByDay();
        return Result.success(nums);
    }

    @ApiOperation("/获取日志总数量")
    @GetMapping("/getCount")
    public Result<Integer> getCount() {
        Integer num = logService.getCount();
        return Result.success(num);
    }
}
